/**
 * @program: LeetCode
 * @description: LeetCode :
 * @author: WXY
 * @create: 2023-01-04 19:45
 * @Version 1.0
 **/
public class offer_eighten2_convertBST {
    int sum = 0;

    public TreeNode convertBST(TreeNode root) {

        dfs(root);
        return root;
    }

    private int dfs(TreeNode root) {
        if (root == null) return 0;
        dfs(root.right);
        sum += root.val;
        root.val = sum;
        dfs(root.left);
        return sum;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
